package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.HmzDoor;
import com.ruoyi.system.domain.HmzProduct;
import com.ruoyi.system.service.IHmzDoorService;
import com.ruoyi.system.service.IHmzProductService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/discrete")
public class DiscreteController {

    @Autowired
    private IHmzProductService hmzProductService;
    @Autowired
    private IHmzDoorService hmzDoorService;
    /**
     * 连接设备 发卡器
     * @param DK
     * @param url
     * @return
     * @throws IOException
     */
    @GetMapping("/connect")
    public AjaxResult connectToDevice(String DK,String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String api =url+"/api/discrete/connect?DK="+DK;
        Request request = new Request.Builder()
                .url(api)
                .get()
                .build();
        String responseBody="";
//        Response response = client.newCall(request).execute();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                responseBody = response.body().string();
                System.out.println("Response Body: " + responseBody);
                // 这里可以根据实际情况解析 responseBody
            } else {
                System.out.println("Request failed with code: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success(responseBody);
    }

    /**
     * , @RequestBody HmzProduct hmzProduct 读取标签 发卡器
     * @param url
     * @param hmzProduct
     * @return
     */
    @PostMapping("/read")
    public AjaxResult readSingleTag( @RequestBody HmzProduct hmzProduct) {
      String  url = hmzProduct.getUrl();
        OkHttpClient client = new OkHttpClient();
       String api = url+"/api/discrete/read";
        Request request = new Request.Builder()
                .url(api)
                .get()
                .build();
        String responseBody="";
//        Response response = client.newCall(request).execute();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                responseBody = response.body().string();
                response.message();
                if("读取失败".equals(responseBody)){

                }else {
                    hmzProduct.setFkStatus("已发卡");
                    hmzProduct.setRfidNumber(responseBody);
                    hmzProductService.updateHmzProduct(hmzProduct);
                }
                System.out.println("Response Body: " + responseBody);



                // 这里可以根据实际情况解析 responseBody
            } else {
                System.out.println("Request failed with code: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


     return    AjaxResult.success(responseBody);
    }

    /**
     * 断开连接发卡器
     * @param url
     * @return
     * @throws IOException
     */
    @GetMapping("/disconnect")
    public AjaxResult disconnectDevice(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
       String api= url+ "/api/discrete/disconnect";
        Request request = new Request.Builder()
                .url(api)
                .get()
                .build();
        String responseBody="";
//        Response response = client.newCall(request).execute();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                responseBody = response.body().string();
                System.out.println("Response Body: " + responseBody);
                // 这里可以根据实际情况解析 responseBody
            } else {
                System.out.println("Request failed with code: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return    AjaxResult.success(responseBody);
    }

    /**
     * 连接通道门
     * @param url
     * @return
     * @throws IOException
     */
    @GetMapping("/tcpClientTdmOn")
    public AjaxResult tcpClientTdmOn(String url,String ip) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String api =url+"/api/discrete/tcpClientTdmOn?ip="+ip;
        Request request = new Request.Builder()
                .url(api)
                .get()
                .build();
        String responseBody="";
//        Response response = client.newCall(request).execute();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                responseBody = response.body().string();
                System.out.println("Response Body: " + responseBody);
                // 这里可以根据实际情况解析 responseBody
            } else {
                System.out.println("Request failed with code: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String s = responseBody.toString();
        if("连接成功".equals(s)){
            HmzDoor hmzDoor = new HmzDoor();
            hmzDoor.setIp(ip);
            hmzDoor.setStatus("已连接");
            hmzDoorService.updateHmzDoor(hmzDoor);
        }
        return    AjaxResult.success(responseBody);
    }

    /**
     * 断开通道门
     * @param url
     * @return
     * @throws IOException
     */
    @GetMapping("/tcpClientTdmOff")
    public AjaxResult tcpClientTdmOff(String url,String ip) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String api= url+ "/api/discrete/tcpClientTdmOff?ip="+ip;
        Request request = new Request.Builder()
                .url(api)
                .get()
                .build();
        String responseBody="";
//        Response response = client.newCall(request).execute();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                responseBody = response.body().string();
                System.out.println("Response Body: " + responseBody);
                // 这里可以根据实际情况解析 responseBody
            } else {
                System.out.println("Request failed with code: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String s = responseBody.toString();
        if("true".equals(s)){
            HmzDoor hmzDoor = new HmzDoor();
            hmzDoor.setIp(ip);
            hmzDoor.setStatus("未连接");
            hmzDoorService.updateHmzDoor(hmzDoor);
        }
        return    AjaxResult.success(responseBody);
    }


}
